from mcp.server.fastmcp import FastMCP
import sympy as sp
import numpy as np

# 创建 MCP 服务实例
mcp = FastMCP("Advanced Symbolic Matrix Calculator")

@mcp.tool()
def matrix_add(a: str, b: str) -> str:
    """符号矩阵加法"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        mat_b = sp.Matrix(sp.sympify(b))
        return str(mat_a + mat_b)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_subtract(a: str, b: str) -> str:
    """符号矩阵减法"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        mat_b = sp.Matrix(sp.sympify(b))
        return str(mat_a - mat_b)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_multiply(a: str, b: str) -> str:
    """符号矩阵乘法"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        mat_b = sp.Matrix(sp.sympify(b))
        return str(mat_a * mat_b)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def scalar_multiply(a: str, k: str) -> str:
    """符号矩阵数乘"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        scalar = sp.sympify(k)
        return str(mat_a * scalar)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_transpose(a: str) -> str:
    """符号矩阵转置"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.T)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_inverse(a: str) -> str:
    """符号矩阵求逆"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.inv())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_determinant(a: str) -> str:
    """符号矩阵行列式"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.det())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_eigenvals(a: str) -> str:
    """符号矩阵特征值计算"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.eigenvals())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_eigenvects(a: str) -> str:
    """符号矩阵特征向量计算"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.eigenvects())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_lu(a: str) -> str:
    """符号矩阵LU分解"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        L, U, _ = mat_a.LUdecomposition()
        return f"L:\n{str(L)}\n\nU:\n{str(U)}"
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_qr(a: str) -> str:
    """符号矩阵QR分解"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        Q, R = mat_a.QRdecomposition()
        return f"Q:\n{str(Q)}\n\nR:\n{str(R)}"
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_svd(a: str) -> str:
    """符号矩阵奇异值分解(SVD)"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        U, S, V = mat_a.singular_value_decomposition()
        return f"U:\n{str(U)}\n\nS:\n{str(S)}\n\nV:\n{str(V)}"
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_cholesky(a: str) -> str:
    """符号矩阵Cholesky分解"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        L = mat_a.cholesky()
        return str(L)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_jordan(a: str) -> str:
    """符号矩阵Jordan标准形"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        P, J = mat_a.jordan_form()
        return f"P:\n{str(P)}\n\nJ:\n{str(J)}"
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_spectral(a: str) -> str:
    """符号矩阵谱分解"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.spectral_decomposition())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_diagonalize(a: str) -> str:
    """符号矩阵对角化"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        P, D = mat_a.diagonalize()
        return f"P:\n{str(P)}\n\nD:\n{str(D)}"
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_power(a: str, power: str) -> str:
    """符号矩阵的幂"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        p = sp.sympify(power)
        return str(mat_a ** p)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_exp(a: str) -> str:
    """符号矩阵的指数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.exp())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_log(a: str) -> str:
    """符号矩阵的对数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.log())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_sqrt(a: str) -> str:
    """符号矩阵的平方根"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.sqrt())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_norm(a: str, norm_type: str = "2") -> str:
    """符号矩阵的范数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        n_type = sp.sympify(norm_type)
        return str(mat_a.norm(n_type))
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_rank(a: str) -> str:
    """符号矩阵的秩"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.rank())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_nullspace(a: str) -> str:
    """符号矩阵的零空间"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.nullspace())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_columnspace(a: str) -> str:
    """符号矩阵的列空间"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.columnspace())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_rowspace(a: str) -> str:
    """符号矩阵的行空间"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.rowspace())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_trace(a: str) -> str:
    """符号矩阵的迹"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.trace())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_nullspace_basis(a: str) -> str:
    """符号矩阵的零空间基"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.nullspace())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_columnspace_basis(a: str) -> str:
    """符号矩阵的列空间基"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.columnspace())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_rowspace_basis(a: str) -> str:
    """符号矩阵的行空间基"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.rowspace())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def solve_linear_system(a: str, b: str) -> str:
    """符号矩阵的线性方程组求解"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        vec_b = sp.Matrix(sp.sympify(b))
        return str(mat_a.solve(vec_b))
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def least_squares_solution(a: str, b: str) -> str:
    """符号矩阵的最小二乘解"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        vec_b = sp.Matrix(sp.sympify(b))
        return str(mat_a.solve_least_squares(vec_b))
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_condition_number(a: str) -> str:
    """符号矩阵的条件数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.condition_number())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_spectral_radius(a: str) -> str:
    """符号矩阵的谱半径"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.spectral_radius())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_differential(a: str, var: str) -> str:
    """符号矩阵的微分"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        variable = sp.sympify(var)
        return str(mat_a.diff(variable))
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_integral(a: str, var: str) -> str:
    """符号矩阵的积分"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        variable = sp.sympify(var)
        return str(mat_a.integrate(variable))
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_orthogonalize(a: str) -> str:
    """符号矩阵的正交化"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.orthogonalize())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_normalize(a: str) -> str:
    """符号矩阵的标准化"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.normalized())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_exp_func(a: str) -> str:
    """符号矩阵的矩阵指数函数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.exp())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_log_func(a: str) -> str:
    """符号矩阵的矩阵对数函数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.log())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_sqrt_func(a: str) -> str:
    """符号矩阵的矩阵平方根"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.sqrt())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def solve_matrix_ode(a: str, t: str) -> str:
    """符号矩阵的矩阵微分方程求解"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        time = sp.sympify(t)
        return str(mat_a.solve_ode(time))
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_integral_func(a: str) -> str:
    """符号矩阵的矩阵积分"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.integrate())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_derivative(a: str) -> str:
    """符号矩阵的矩阵导数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.diff())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_function(a: str, func: str) -> str:
    """符号矩阵的矩阵函数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        f = sp.sympify(func)
        return str(mat_a.applyfunc(f))
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_polynomial(a: str, coeffs: str) -> str:
    """符号矩阵的矩阵多项式"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        coefficients = [sp.sympify(c) for c in coeffs.split(',')]
        result = sum(c * (mat_a ** i) for i, c in enumerate(coefficients))
        return str(result)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_power_series(a: str, coeffs: str) -> str:
    """符号矩阵的矩阵幂级数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        coefficients = [sp.sympify(c) for c in coeffs.split(',')]
        result = sum(c * (mat_a ** i) for i, c in enumerate(coefficients))
        return str(result)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_rational_func(a: str, num: str, den: str) -> str:
    """符号矩阵的矩阵有理函数"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        numerator = [sp.sympify(c) for c in num.split(',')]
        denominator = [sp.sympify(c) for c in den.split(',')]
        num_result = sum(c * (mat_a ** i) for i, c in enumerate(numerator))
        den_result = sum(c * (mat_a ** i) for i, c in enumerate(denominator))
        return f"Numerator:\n{str(num_result)}\n\nDenominator:\n{str(den_result)}"
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_exp_integral(a: str) -> str:
    """符号矩阵的矩阵指数积分"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.exp().integrate())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_log_integral(a: str) -> str:
    """符号矩阵的矩阵对数积分"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.log().integrate())
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_sqrt_integral(a: str) -> str:
    """符号矩阵的矩阵平方根积分"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.sqrt().integrate())
    except Exception as e:
        return f"Error: {str(e)}"

if __name__ == "__main__":
    mcp.run(transport="stdio")